function setCookie(cookieName, cookieValue, seconds, path, domain, secure) {
	var expires = new Date();
	if(cookieValue == '' || seconds < 0) {
		cookieValue = '';
		seconds = -2592000;
	}
	expires.setTime(expires.getTime() + seconds * 1000);
	domain = COOKIE_DOMAIN;
	path = COOKIE_PATH;
	document.cookie = escape(COOKIE_PRE + cookieName) + '=' + escape(cookieValue)
		+ (expires ? '; expires=' + expires.toGMTString() : '')
		+ (path ? '; path=' + path : '/')
		+ (domain ? '; domain=' + domain : '')
		+ (secure ? '; secure' : '');
}

function getCookie(name)
{
	 var arr,reg=new RegExp("(^| )"+COOKIE_PRE+name+"=([^;]*)(;|$)");
	 if(arr=document.cookie.match(reg))
	  return unescape(arr[2]);
	 else
	  return false;
}
$(function() {
	$('#list tr:even').addClass('bg2');
	$('#list tr').hover( function () { $(this).addClass('tr_hover'); } , function () { $(this).removeClass('tr_hover'); } );
	$('#list :checkbox').each(function(){
		$(this).click(function() {
			var id = $(this).val();
			if ($(this).attr('checked')) {
				$('#row_'+id).addClass('highlight');
			} else {
				$('#row_'+id).removeClass('highlight');
			}
		});
	});
	if(getCookie('msg')) {
		var msg = decodeURI(getCookie('msg'));
		setCookie('msg','');
		$('<div class="sucmsg">'+msg+'</div>').insertAfter($('.heading'));
		setTimeout("$('.sucmsg').fadeOut(1000)",3000);
	}
});

function check_all(ck) {
	if ($(ck).attr('checked')) {
		$("form :input[name='id[]']").each(function() {
			$(this).attr('checked','checked');
			$(this).parent().parent().addClass('highlight');
		});
	} else {
		$("form :input[name='id[]']").each(function() {
			$(this).removeAttr('checked');
			$(this).parent().parent().removeClass('highlight');
		});
	}
}

function do_submit(f,form) {
	$('#f').val(f);
	form.submit();
}

function checkalltable(obj) {
	if (obj.checked) {
		$(":input[name='baktables[]']").each(function(){
			$(this).attr('checked','checked');
		});
	} else {
		$(":input[name='baktables[]']").each(function(){
			$(this).removeAttr('checked');
		});
	}
}
//保存用户组
function usergroup_save() {
	if ($.trim($("form :input[name='groupname']").val()) == '') {
		error('请输入用户组名称');
		return false;
	}
	return true;
}
function user_save() {
	var password = $.trim($("form :input[name='password']").val());
	var password2 = $.trim($("form :input[name='password2']").val());
	var userid = $.trim($("form :input[name='uid']").val());

	if ($.trim($("form :input[name='username']").val()) == '') {
		error('请输入用户名');
		$("form :input[name='username']").focus();
	} else if ( password == '' && !userid) {
		error('请输入密码');
		$("form :input[name='password']").focus();		
	} else if ( password != password2 ) {
		error('两次输入的密码不一致');
		$("form :input[name='password2']").focus();		
	} else {
		check_username();
		$("form").submit();
	}
}
//用户审核
function check_user(id) {
	$('#check_a_'+id).html('<img src="admin/template/images/loading.gif" />');
	$.ajax({
		type : 'GET',
		url : 'admin.php?m=user&f=ajax_check_user&uid='+id,
		cache : false,
		success : function(msg) {
			eval("var message = "+msg);
			if (message.type == 'success') {
				if (message.data == 'check') {
					$('#row_'+id).removeClass('highlight');
					$('#check_a_'+id).text('已通过');
				} else {
					$('#row_'+id).addClass('highlight');
					$('#check_a_'+id).html('<span style="color:red">未通过</span>');
				}
			} else {
				alert(message.data);
				$('#check_a_'+id).text('已通过');
			}			
		}
	});
}
//检查用户名
function check_username() {
	var username = $.trim($("form :input[name='username']").val());
	if (username=='') return false;
	$.get('admin.php','m=user&f=ajax_check_username&username='+encodeURIComponent(username),function(msg) {
		if (msg == 'success') {
			$('#msg_username').html('<font color="green">可用</font>');
			$('#save_user').removeAttr('disabled');
		} else if (msg == 'error') {
			$('#msg_username').html('<font color="red">该用户名已经被使用</font>');
			$('#save_user').attr('disabled','disabled');
		}
	});
}
//保存标签
function check_tag_add() {
	if ( $(":input[name='tagname']").val() == '' ) {
		alert('请输入标签名称。');
		$(":input[name='tagname']").focus();
		return false;
	}
	return true;
}
//删除链接组
function delete_linkgroup(id) {
	if (id) {
		$.get('admin.php','m=linkgroup&f=delete&id='+id,function(msg) {
			if (msg == 'success') {
				$('#linkgroup_'+id).attr('class','highlight');
				$('#linkgroup_'+id).fadeOut('slow');
			} else {
				alert(msg);
			}
		});
	}	
}
//保存链接
function save_link() {
	if ($.trim($("form :input[name='name']").val()) == '') {
		error('请输入链接名称');
		return false;
	} else if ($("form :input[name='url']").val() == '') {
		error('请输入链接地址');
		return false;
	} else {
		$(":submit").attr('disabled','disabled');
		$(":submit").val('正在保存...');
		return true;
	}
}
//------文章------
function set_tags_on() {
	var tags = $('#tags').val();
	$('#utags a').each(function() {
		if (tags.indexOf($(this).text()) != -1) {
			$(this).addClass('on');
		}
	});
}

function check_write() {
	if ($.trim($(":input[name='title']").val()) == '') {
		alert('请填写文章标题');
		$(":input[name='title']").focus();
		return false;
	}
	if ($(":input[name='cid'] :selected").val() == 0) {
		alert('请选择文章类别');
		$(":input[name='cid']").focus();
		return false;
	}
	return true;
}

function save_to_draft() {
	$('#todraft').val(1);
	if (check_write()) {
		$('form').submit();
	}
}

function check_urlname() {
	var urlname = $.trim($(":input[name='urlname']").val());
	var id = $.trim($(":input[name='id']").val());
	$.ajax({
		type:'POST',
		url:'admin.php?m=article&f=check_urlname',
		data:'urlname='+encodeURIComponent(urlname)+'&id='+id,
		cache:false,
		dataType:'json',
		success:function(msg) {
			if ( msg.data != '' ) { 
				if (msg.type == 'success') {
					$('#tip_urlname').css('color','green');
				} else {
					$('#tip_urlname').css('color','red');
				}
				$('#tip_urlname').text(msg.data);
			}
			if(msg.alias != ''){
				$('#urlname').val(msg.alias);
			}
		}
	});
}

function insert_attach(val){
	editor.insertHtml(val);
}

function del_attach(id) {
	if(!confirm('确定删除？')) return false;
	if(id){
		$.getJSON('index.php?m=upload&f=delete&id='+id+'&tm='+Math.random(),'',function(data){
			if(data.s==true){
				$('#tr_'+id).remove();
			}else{
			}
		});
	}
}
function to_front(id,aid) {
	$.ajax({
		type:'POST',
		url:'admin.php?m=article&f=to_front&id='+id+'&aid='+aid,
		cache:false,
		success:function(msg) {
			if (msg == 'success') {
				$("#tr_"+id).addClass('highlight');
				//$("#tr_"+id).fadeOut('slow');
				alert('成功设为封面,如果此图片你还没有使用，你需要保存一下内容。')
			}
		}
	});
}
function del_front(id) {
	if(!confirm('确定取消封面？')) return false;
	$.ajax({
		type:'POST',
		url:'admin.php?m=article&f=del_front&aid='+id,
		cache:false,
		success:function(msg) {
			if (msg == 'success') {
				$("#tr_"+id).addClass('highlight');
				alert('成功取消封面,需要刷新下才能看到结果。')
			}
		}
	});
}
//文章分类
function category_save() {
	if ($.trim($("form :input[name='catename']").val()) == '') {
		$('<div class="sucmsg">请输入分类名称</div>').insertAfter($('.heading'));
		setTimeout("$('.sucmsg').fadeOut(1000)",3000);
		return false;
	}
	$(":submit").attr('disabled','disabled');
	$(":submit").val('正在保存...');
	return true;
}
//评论
function delete_comment(id) {
	if (id) {
		$.get('admin.php','m=comment&f=delete&id='+id,function(msg) {
			if (msg == 'success') {
				$('#row_'+id).attr('class','highlight');
				$('#row_'+id).fadeOut('slow');
			} else {
				alert(msg);
			}
		});
	}
}
function check_comment(id) {
	var count = parseInt($('#comment_count').text());
	$.ajax({
		type:'GET',
		url:'admin.php?m=comment&f=ajax_check&id='+id,
		cache:false,
		dataType:'json',
		success:function(message) {
			if (message.type == 'success') {
				if (message.data == 'check') {
					$('#comment_count').text(count-1);
					$('#row_'+id).removeClass('highlight');
					$('#check_a_'+id).text('不审核');
				} else {
					$('#comment_count').text(count+1);
					$('#row_'+id).addClass('highlight');
					$('#check_a_'+id).text('审核');
				}
			} else {
				alert(message.data);
			}
		}
	});
}
function edit_comment(id) {
	if ( $("#edit_"+id).val() !== undefined ) return false;
	var content = $("#c_"+id).html().replace(/<BR>/g,"\n");
	$("#c_"+id).css('display','none');
	$("#e_"+id).css('display','block');
	$("#e_"+id).html('<form name="edit" method="post" action=""><input type="hidden" name="id" value="'+id+'" /><textarea name="content" cols="60" rows="5">'+content+'</textarea> <input type="button" class="submit" value="更新" onclick="edit_submit(this.form)" /> <input type="button" class="submit" value="返回" onclick="edit_back('+id+')" /></form>');
}

function edit_back(id) {
	$("#c_"+id).css('display','block');
	$("#e_"+id).css('display','none');
}
function edit_submit(form) {
	var id=form.id.value;
	var content = form.content.value;
	if ( content == '' ) {
		alert('请输入内容'); return false;
	}
	$.ajax({
		type:'POST',
		url:'admin.php?m=comment&f=ajax_edit',
		cache:false,
		data:'id='+id+'&content='+encodeURIComponent(content),
		dataType:'json',
		success:function(message){
			if ( message.type == 'success' ) {
				$("#c_"+id).html(content.replace(/\n/g, '<br />'));
				$("#c_"+id).css('display','block');
				$("#e_"+id).css('display','none');
			} else {
				alert(message.data);
			}
		}
	});
}

//删除自定义变量
function del_myvar(varname) {
	if ( confirm('一旦删除将不可恢复，确定吗？') ) {
		$.ajax({
			type:'GET',
			url : 'admin.php?m=config&f=delmyvar&varname='+encodeURIComponent(varname),
			dataType:'json',
			success:function(message) {
				if ( message.type == 'success' ) {
					$("#myvar_"+varname).fadeOut("slow");
				} else {
					alert(message.data);
				}
			}
		});
	}
	return false;
}
function setCopy(text) {
	 if (window.clipboardData) {
		    window.clipboardData.setData("Text", text);
			alert("已经成功复制到剪贴板！");
		  } else {
			var x=prompt('你的浏览器不支持自动复制,请你手动复制下面的地址(Ctrl+C)：',text);
		  }
	}

/*
 * Superfish v1.4.8 - jQuery menu widget
 * Copyright (c) 2008 Joel Birch
 *
 * Dual licensed under the MIT and GPL licenses:
 * 	http://www.opensource.org/licenses/mit-license.php
 * 	http://www.gnu.org/licenses/gpl.html
 *
 * CHANGELOG: http://users.tpg.com.au/j_birch/plugins/superfish/changelog.txt
 */

;(function($){
	$.fn.superfish = function(op){

		var sf = $.fn.superfish,
			c = sf.c,
			$arrow = $(['<span class="',c.arrowClass,'"> &#187;</span>'].join('')),
			over = function(){
				var $$ = $(this), menu = getMenu($$);
				clearTimeout(menu.sfTimer);
				$$.showSuperfishUl().siblings().hideSuperfishUl();
			},
			out = function(){
				var $$ = $(this), menu = getMenu($$), o = sf.op;
				clearTimeout(menu.sfTimer);
				menu.sfTimer=setTimeout(function(){
					o.retainPath=($.inArray($$[0],o.$path)>-1);
					$$.hideSuperfishUl();
					if (o.$path.length && $$.parents(['li.',o.hoverClass].join('')).length<1){over.call(o.$path);}
				},o.delay);	
			},
			getMenu = function($menu){
				var menu = $menu.parents(['ul.',c.menuClass,':first'].join(''))[0];
				sf.op = sf.o[menu.serial];
				return menu;
			},
			addArrow = function($a){ $a.addClass(c.anchorClass).append($arrow.clone()); };
			
		return this.each(function() {
			var s = this.serial = sf.o.length;
			var o = $.extend({},sf.defaults,op);
			o.$path = $('li.'+o.pathClass,this).slice(0,o.pathLevels).each(function(){
				$(this).addClass([o.hoverClass,c.bcClass].join(' '))
					.filter('li:has(ul)').removeClass(o.pathClass);
			});
			sf.o[s] = sf.op = o;
			
			$('li:has(ul)',this)[($.fn.hoverIntent && !o.disableHI) ? 'hoverIntent' : 'hover'](over,out).each(function() {
				if (o.autoArrows) addArrow( $('>a:first-child',this) );
			})
			.not('.'+c.bcClass)
				.hideSuperfishUl();
			
			var $a = $('a',this);
			$a.each(function(i){
				var $li = $a.eq(i).parents('li');
				$a.eq(i).focus(function(){over.call($li);}).blur(function(){out.call($li);});
			});
			o.onInit.call(this);
			
		}).each(function() {
			menuClasses = [c.menuClass];
			if (sf.op.dropShadows  && !($.browser.msie && $.browser.version < 7)) menuClasses.push(c.shadowClass);
			$(this).addClass(menuClasses.join(' '));
		});
	};

	var sf = $.fn.superfish;
	sf.o = [];
	sf.op = {};
	sf.IE7fix = function(){
		var o = sf.op;
		if ($.browser.msie && $.browser.version > 6 && o.dropShadows && o.animation.opacity!=undefined)
			this.toggleClass(sf.c.shadowClass+'-off');
		};
	sf.c = {
		bcClass     : 'sf-breadcrumb',
		menuClass   : 'sf-js-enabled',
		anchorClass : 'sf-with-ul',
		arrowClass  : 'sf-sub-indicator',
		shadowClass : 'sf-shadow'
	};
	sf.defaults = {
		hoverClass	: 'sfHover',
		pathClass	: 'overideThisToUse',
		pathLevels	: 1,
		delay		: 800,
		animation	: {opacity:'show'},
		speed		: 'normal',
		autoArrows	: true,
		dropShadows : true,
		disableHI	: false,		// true disables hoverIntent detection
		onInit		: function(){}, // callback functions
		onBeforeShow: function(){},
		onShow		: function(){},
		onHide		: function(){}
	};
	$.fn.extend({
		hideSuperfishUl : function(){
			var o = sf.op,
				not = (o.retainPath===true) ? o.$path : '';
			o.retainPath = false;
			var $ul = $(['li.',o.hoverClass].join(''),this).add(this).not(not).removeClass(o.hoverClass)
					.find('>ul').hide().css('visibility','hidden');
			o.onHide.call($ul);
			return this;
		},
		showSuperfishUl : function(){
			var o = sf.op,
				sh = sf.c.shadowClass+'-off',
				$ul = this.addClass(o.hoverClass)
					.find('>ul:hidden').css('visibility','visible');
			sf.IE7fix.call($ul);
			o.onBeforeShow.call($ul);
			$ul.animate(o.animation,o.speed,function(){ sf.IE7fix.call($ul); o.onShow.call($ul); });
			return this;
		}
	});

})(jQuery);
$.fn.tabs = function() {
	var selector = this;
	this.each(function() {
		var obj = $(this); 
		$(obj.attr('id')).hide();
		$(obj).click(function() {
			$(selector).removeClass('selected');
			$(selector).each(function(i, element) {
				$($(element).attr('id')).hide();
			});
			$(this).addClass('selected');
			$($(this).attr('id')).show();
			return false;
		});
	});
	$(this).show();
};
function getalias(obj){
	if(obj.checked){
		$('#urlname').val($('#title').val());
		check_urlname();
	}else{
		$('#urlname').val("");
	}
	
}
function error(msg,t){
	$('<div class="sucmsg">'+msg+'</div>').insertAfter($('.heading'));
	setTimeout("$('.sucmsg').fadeOut(1000)",t>0?t:3000);
	setTimeout("$('.sucmsg').remove()",t>0?t:4000);

}